package com.winning.devops.demo.controller;

import com.winning.devops.demo.exception.UserNotExistsException;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author chensj
 * @title Controller 异常处理
 * @project winning-spring-security
 * @package com.winning.devops.demo.controller
 * @date: 2019-05-14 23:04
 */
@ControllerAdvice
public class ControllerExceptionHandler {


    /**
     * 处理指定异常
     * 返回500 状态码
     * @return
     */
    @ExceptionHandler(UserNotExistsException.class)
    @ResponseBody
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public Map<String,Object> handlerUserNotException(UserNotExistsException ex){
        Map<String,Object> errorMap = new HashMap<>();
        errorMap.put("message",ex.getMessage());
        errorMap.put("id",ex.getId());
        errorMap.put("timestamp",new Date());
        return errorMap;
    }

}
